package com.cloudsigns.factorymanager2.produce.fragment.produce_team;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamDailyFeedbackActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamDailyFeedbackApplyAdjustmentActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamDailyFeedbackApplyReworkActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamDailyFeedbackApplyScrapActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamDailyFeedbackDetailActivity;
import com.cloudsigns.factorymanager2.produce.activity.produce_team.ProduceTeamFeedbackReplyForTeamActivity;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.IFeedbackDailyItemListener;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.ProduceTeamOrderDailyFeedbackAdapter;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.ProduceTeamOrderDailyFeedbackDateAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartDailyFeedbackBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderResponse;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.produce.bean.request.DailyFeedbackDateItem;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ProduceTeamOrderDailyFeedbackFragment extends Fragment implements View.OnClickListener, ItemClickListener, IFeedbackDailyItemListener {
    private String TAG = ProduceTeamOrderDailyFeedbackFragment.class.getSimpleName();
    private TextView tv_abnormal_return;
    private ImageView img_abnormal_return;
    private ScrollView scrollview;
    private ListViewForScrollView lv_abnormal_return;
    //    private LinearLayout ll_feedback;
    private ListViewForScrollView ll_feedback;
    private Button btn_adjustment, btn_rework, btn_scrap;

    private WorkPartOrderBean workPartOrderBean;
    private WorkPartBean workPartBean;

    private List<String> dayStrList;
    private HashMap<String, List<WorkPartDailyFeedbackBean>> feedBackMap;
    private LayoutInflater layoutInflater;
    private ProduceTeamOrderDailyFeedbackAdapter replayAdapter;// 异常回复
    private ProduceTeamOrderDailyFeedbackDateAdapter dailyFeedbackAdapter;// 日期列表
    private List<WorkPartDailyFeedbackBean> repalyDataList = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_produce_team_daily_feedback, null);
        tv_abnormal_return = view.findViewById(R.id.tv_abnormal_return);
        img_abnormal_return = view.findViewById(R.id.img_abnormal_return);
        scrollview = view.findViewById(R.id.scrollview);
        lv_abnormal_return = view.findViewById(R.id.lv_abnormal_return);
        ll_feedback = view.findViewById(R.id.ll_feedback);
        btn_adjustment = view.findViewById(R.id.btn_adjustment);
        btn_rework = view.findViewById(R.id.btn_rework);
        btn_scrap = view.findViewById(R.id.btn_scrap);

        img_abnormal_return.setOnClickListener(this);
        btn_adjustment.setOnClickListener(this);
        btn_rework.setOnClickListener(this);
        btn_scrap.setOnClickListener(this);

        tv_abnormal_return.setText(getResources().getString(R.string.abnormal_return));
        img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);
        layoutInflater = inflater;

        replayAdapter = new ProduceTeamOrderDailyFeedbackAdapter(getActivity(), ProduceTeamOrderDailyFeedbackAdapter.VISABLETYPE_TOP);
        replayAdapter.setData(repalyDataList);
        replayAdapter.setItemClick(new ItemClickListener() {
            @Override
            public void itemClick(View view, int position) {
                WorkPartDailyFeedbackBean workPartDailyFeedbackBean = repalyDataList.get(position);
                Intent mIntent = new Intent(getActivity(), ProduceTeamFeedbackReplyForTeamActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartDailyFeedbackBean);
                startActivity(mIntent);
            }
        });
        lv_abnormal_return.setAdapter(replayAdapter);


        dailyFeedbackAdapter = new ProduceTeamOrderDailyFeedbackDateAdapter(getActivity());
        ll_feedback.setAdapter(dailyFeedbackAdapter);

        dailyFeedbackAdapter.setItemClickListener(this);
        dailyFeedbackAdapter.setDailyItemClickListener(this);
        return view;
    }

    // 日期列表展示
    private void updateUi() {
        if (null != dayStrList && null != ll_feedback) {
            List<DailyFeedbackDateItem> dailyItemList = new ArrayList<>();

            for (String date : dayStrList) {
                DailyFeedbackDateItem dailyItem = new DailyFeedbackDateItem();

                dailyItem.setStrDate(date);

                if (feedBackMap.containsKey(date)) {
                    dailyItem.workPartDailyFeedbackBeanList.addAll(feedBackMap.get(date));
                } else {
                    WorkPartDailyFeedbackBean workPartDailyFeedbackBean = new WorkPartDailyFeedbackBean();
                    workPartDailyFeedbackBean.setNoFeedBack(true);
                    dailyItem.workPartDailyFeedbackBeanList.add(workPartDailyFeedbackBean);
                }
                dailyItemList.add(dailyItem);
            }
            dailyFeedbackAdapter.setData(dailyItemList);
            dailyFeedbackAdapter.notifyDataSetChanged();


          /*    ll_feedback.removeAllViews();
          for (String date : dayStrList) {
                View view = layoutInflater.inflate(R.layout.item_produce_team_order_daily_feedback, null);
                TextView tv_date = view.findViewById(R.id.tv_date);
                TextView tv_week = view.findViewById(R.id.tv_week);
                ImageView img_add_feedback = view.findViewById(R.id.img_add_feedback);
                ListView lv_feedback = view.findViewById(R.id.lv_feedback);
                tv_date.setText(date);
                String week = DateUtil.getWeekByDateStr(date);
                tv_week.setText(week);
                img_add_feedback.setOnClickListener(this);

                long currentMill = System.currentTimeMillis();
                String currentDate = DateUtil.getYyyy_Mm_Dd(currentMill);
                if (date.equals(currentDate)) {
                    img_add_feedback.setImageResource(R.mipmap.icon_add_feedback_pressed);
                    img_add_feedback.setEnabled(true);
                } else {
                    img_add_feedback.setImageResource(R.mipmap.icon_add_feedback_normal);
                    img_add_feedback.setEnabled(false);
                }

                final List<WorkPartDailyFeedbackBean> dataList = new ArrayList<>();

                if (feedBackMap.containsKey(date)) {
                    dataList.addAll(feedBackMap.get(date));
                } else {
                    WorkPartDailyFeedbackBean workPartDailyFeedbackBean = new WorkPartDailyFeedbackBean();
                    workPartDailyFeedbackBean.setNoFeedBack(true);
                    dataList.add(workPartDailyFeedbackBean);
                }
                ProduceTeamOrderDailyFeedbackAdapter adapter = new ProduceTeamOrderDailyFeedbackAdapter(getActivity(), ProduceTeamOrderDailyFeedbackAdapter.VISABLETYPE_NORMAL);
                adapter.setData(dataList);
                adapter.setItemClick(this);
                lv_feedback.setAdapter(adapter);
                ll_feedback.addView(view);
            }*/
        }
        scrollview.smoothScrollTo(0, 0);
    }

    public void initData(WorkPartBean workPartBean, WorkPartOrderResponse workPartOrderResponse) {
        if (null != workPartBean) {
            this.workPartBean = workPartBean;
            this.workPartOrderBean = workPartBean.getWorkPartOrderById(workPartOrderResponse.getWorkPartOrderId());
            feedBackMap = new HashMap<>();
            List<WorkPartDailyFeedbackBean> allWorkPartDailyFeedbackList = workPartOrderBean.getWorkPartDailyFeedbackList();

            if (null != allWorkPartDailyFeedbackList) {
                repalyDataList.clear();
                feedBackMap.clear();

                for (WorkPartDailyFeedbackBean workPartDailyFeedbackBean : allWorkPartDailyFeedbackList) {

                    CommonEnum.FeedbackStatus unusualStatus = workPartDailyFeedbackBean.getFeedbackStatus();
                    if (null != unusualStatus) {

                        CommonEnum.WorkPartType type = workPartDailyFeedbackBean.getType();
                        // 异常回复
                        if (type == CommonEnum.WorkPartType.UNUSUAL) {
                            if (unusualStatus == CommonEnum.FeedbackStatus.CONFIRM_FINISH || unusualStatus == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM || unusualStatus == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH)
                                repalyDataList.add(workPartDailyFeedbackBean);
                        }
                    }
                    String key = DateUtil.getYyyy_Mm_Dd(workPartDailyFeedbackBean.getFeedbackDate());
                    if (feedBackMap.containsKey(key)) {
                        feedBackMap.get(key).add(workPartDailyFeedbackBean);
                    } else {
                        List<WorkPartDailyFeedbackBean> workPartDailyFeedbackBeanList = new ArrayList<>();
                        workPartDailyFeedbackBeanList.add(workPartDailyFeedbackBean);
                        feedBackMap.put(key, workPartDailyFeedbackBeanList);
                    }
                }
                replayAdapter.setData(repalyDataList);
                replayAdapter.notifyDataSetChanged();
            }

            long receiveOrderTime = workPartOrderBean.getReceiveAssignOrderTime();
            long currentTime = System.currentTimeMillis();

            Date beginDate = new Date(receiveOrderTime);
            Date currentDate = new Date(currentTime);

            int days = DateUtil.differentDays(beginDate, currentDate);
            dayStrList = new ArrayList<>();
            if (days >= 0 && receiveOrderTime > 0) {
                for (int i = days; i >= 0; i--) {
                    long dayTime = i * 24 * 60 * 60 * 1000L;
                    long nextDate = receiveOrderTime + dayTime;
                    dayStrList.add(DateUtil.getYyyy_Mm_Dd(nextDate));
                }
            }
            updateUi();
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            // 添加反馈
//            case R.id.img_add_feedback:
//                if (null != workPartOrderBean) {
//                    intent = new Intent(getActivity(), ProduceTeamDailyFeedbackActivity.class);
//                    intent.putExtra(ConstantUtil.KEY_1, workPartBean);
//                    intent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
//                    startActivity(intent);
//                }
//                break;
            case R.id.img_abnormal_return:
                if (lv_abnormal_return.getVisibility() == View.VISIBLE) {
                    lv_abnormal_return.setVisibility(View.GONE);
                    img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);
                } else {
                    lv_abnormal_return.setVisibility(View.VISIBLE);
                    img_abnormal_return.setBackgroundResource(R.mipmap.arrow_top);
                }
                break;
            // 申请调整
            case R.id.btn_adjustment:
                intent = new Intent(getActivity(), ProduceTeamDailyFeedbackApplyAdjustmentActivity.class);
                intent.putExtra(ConstantUtil.KEY_1, workPartBean);
                intent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                startActivity(intent);
                break;
            // 申请返工
            case R.id.btn_rework:
                intent = new Intent(getActivity(), ProduceTeamDailyFeedbackApplyReworkActivity.class);
                intent.putExtra(ConstantUtil.KEY_1, workPartBean);
                intent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                startActivity(intent);
                break;
            // 申请报废
            case R.id.btn_scrap:
                intent = new Intent(getActivity(), ProduceTeamDailyFeedbackApplyScrapActivity.class);
                intent.putExtra(ConstantUtil.KEY_1, workPartBean);
                intent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
//        WorkPartDailyFeedbackBean workPartDailyFeedbackBean = (WorkPartDailyFeedbackBean) view.getTag();
//        if (null != workPartDailyFeedbackBean) {
//            if (workPartDailyFeedbackBean.isNoFeedBack()) {
//
//            } else {
//                Intent mIntent = new Intent(getActivity(), ProduceTeamDailyFeedbackDetailActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
//                mIntent.putExtra(ConstantUtil.KEY_3, workPartDailyFeedbackBean);
//                WorkPartScheduleBean sbean = workPartBean.getScheduleBeanByTime(workPartDailyFeedbackBean.getFeedbackDate());
//                int count = 0;
//                if (sbean != null) {
//                    count = sbean.getAmount();
//                }
//                mIntent.putExtra(ConstantUtil.KEY_4, count);
//                startActivity(mIntent);
//            }
//        }

        switch (view.getId()) {
            // 添加反馈
            case R.id.img_add_feedback:
                if (null != workPartOrderBean) {
                    Intent intent = new Intent(getActivity(), ProduceTeamDailyFeedbackActivity.class);
                    intent.putExtra(ConstantUtil.KEY_1, workPartBean);
                    intent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                    startActivity(intent);
                }
                break;
        }
    }

    // listview item 点击事件
    @Override
    public void onDailyItemClick(WorkPartDailyFeedbackBean workPartDailyFeedbackBean) {
        if (null != workPartDailyFeedbackBean) {
            if (workPartDailyFeedbackBean.isNoFeedBack()) {

            } else {
                Intent mIntent = new Intent(getActivity(), ProduceTeamDailyFeedbackDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                mIntent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
                mIntent.putExtra(ConstantUtil.KEY_3, workPartDailyFeedbackBean);
                WorkPartScheduleBean sbean = workPartBean.getScheduleBeanByTime(workPartDailyFeedbackBean.getFeedbackDate());
                int count = 0;
                if (sbean != null) {
                    count = sbean.getAmount();
                }
                mIntent.putExtra(ConstantUtil.KEY_4, count);
                startActivity(mIntent);
            }
        }
    }
}
